// pages/harddata/harddata.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code:"",
    total_walk: "xx",
    device_code: "???",
    diedao_alarm: '跌倒报警',
    sbxx: "",
    jiuzuo_alarm: {},
    jiuzuo_text:"未久坐",
    dataLoaded:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 100000 // 设置较长的持续时间，以便在加载过程中持续显示
    });
    // 导航栏
    // 1.获取屏幕可使用宽度
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    // 2.获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    //3.获取胶囊按钮位置信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    this.setData({
      navtop:menuButton.top
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){

    const app =getApp()
    const that =this
    wx.login({
      success: res => {
        app.globalData.id=res.code
        wx.request({
          url: 'https://dj.ifankui.com/api/wx/autoLogin',
          method:"POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data:{
              code:app.globalData.id,
          },
          success(res){
              app.globalData.openid=res.data.openid
              console.log(app.globalData.openid);
              that.getDate2()
          }
        })
      }
    })
   
  },

  getDate2(){
    const that = this
    const app =getApp()
    this.setData({
      openid:app.globalData.openid
    })
    console.log(app.globalData.openid);
        wx.request({
    url: 'https://dj.ifankui.com/api/usr/deviceList',
    method:"POST",
    data:{
      openid:this.data.openid
    },
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    success:(res)=>{
      console.log(res);
      if(!res.data.data[0]){
        wx.hideToast();
        return 
      }
      this.setData({
        sbxx:res.data.data[0].device_code,
      },() => {

        
        wx.request({
          url: 'https://dj.ifankui.com/api/device/getDeviceInfo',
          method: "POST",
          data: {
            openid: this.data.openid,
            device_code: this.data.sbxx,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: (res) => {
            console.log(res.data.device);
            let ppp={}
            if(!res.data.device.jiuzuo_alarm[0]){
                ppp=1
            }
            else {
              ppp = res.data.device.jiuzuo_alarm[0];
            }
            that.setData({
              Batterysvalue: res.data.device.battery,
              device_code: res.data.device.device_code,
              total_walk: res.data.device.total_walk,
              diedao_alarm: res.data.device.diedao_alarm,
              jiuzuo_alarm: ppp,
              dataLoaded:true
            }, () => {
              wx.hideToast();
              const inputDate = that.data.jiuzuo_alarm.dt
              console.log(inputDate);
              function inputDate11(inputDate) {
                if(inputDate){
                  let inputDate1 = inputDate.replace(/-/g, '/');
                  console.log(111);
                  return inputDate1
                }
                else {
                  let inputDate1 = ""
                  return inputDate1
                }
              }

              let inputDate1=inputDate11(inputDate)
              if(inputDate1.length>0){                
                const date = new Date(inputDate1);
                // 获取当前时间
                const now = new Date();
                console.log(now,date);
                // 计算时间差（单位：毫秒）
                const diffInMillis = now - date;
                // 将毫秒转换为分钟
                const diffInMinutes = Math.floor(diffInMillis / (1000 * 60));
                // 计算小时数
                const hours = Math.floor(diffInMinutes / 60);
                // 计算剩余分钟数
                const minutes = diffInMinutes % 60;
                that.setData({
                  jiuzuo_text:hours+"小时"+minutes+"分"+"前"
                })}

              
              let percentageNumber = parseFloat(res.data.device.battery.replace('%', ''));
              console.log(percentageNumber);
              if (percentageNumber < 20) {

                wx.showModal({
                  title: '警告',
                  content: '当前电量低于20%',
                  showCancel: false, // 是否显示取消按钮
                  confirmText: '确定', // 确定按钮的文字
                  confirmColor: '#fb3660', // 确定按钮的文字颜色
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                      // 用户点击确定后的操作
                    } else if (res.cancel) {
                      console.log('用户点击取消');
                      // 用户点击取消后的操作
                    }
                  }
                });

              }


            })
          }
        })
      })
      
    }
  
  })

  },

  githui(){
    wx.switchTab({
      url: '/pages/Member/Member',
    })
  },
  bangding() {
    wx.navigateTo({
      url: '../oldcard/oldcard',
    })
  },
})